import * as THREE from "three"
import Vectors from "../three/Vectors"
import CameraControlsBase from "./CameraControlsBase"
import r from "../packages/57812"
export default class OrthographicCameraControls extends CameraControlsBase {
  angularAccel: number
  angularVelocity: number
  constructor(e, t, i, n = !1) {
    super(e, t, i, n)
    this.cameraModule = e
    this.angularAccel = 0
    this.angularVelocity = 0
    this.transition.angularVelocity = 0
  }
  setRollAcceleration(e: number, t = !1) {
    this.transition.active ||
      (t && e && this.angularVelocity && Math.sign(e) !== Math.sign(this.angularVelocity) && (this.angularVelocity = 0), (this.angularAccel = e))
  }
  startRotateTransition(duration: number, t: THREE.Vector2, easeOut: boolean) {
    return this.startTransition(duration, t.x * r.SI, new THREE.Vector2(), 0, easeOut).nativePromise()
  }
  startTransition(duration: number, angularVelocity: number, linearVelocity: THREE.Vector2, zoomVelocity: number, easeOut: boolean) {
    this.transition.angularVelocity = angularVelocity
    this.angularVelocity = angularVelocity
    this.angularAccel = 0
    return super.startTransition(duration, angularVelocity, linearVelocity, zoomVelocity, easeOut)
  }
  update(e: number) {
    super.update(e)
    ;(Math.abs(this.angularAccel) > 0.00001 || Math.abs(this.angularVelocity) > 0.00001) &&
      (this.transition.active ? this.updateAngularTransition(e) : this.updateAngularDefault(e))
  }
  stopMomentum() {
    super.stopMomentum()
    this.transition.active || (this.angularVelocity = 0)
  }
  stopAcceleration() {
    super.stopAcceleration()
    this.transition.active || this.setRollAcceleration(0)
  }
  updateAngularTransition(e) {
    const t = this.getTransitionScale(e)
    this.angularVelocity = this.transition.angularVelocity * t
    this.roll(this.angularVelocity)
  }
  updateAngularDefault(e) {
    const t = e / r.SI
    this.angularVelocity = this.angularVelocity + this.angularAccel * t
    this.roll(this.angularVelocity)
    this.angularVelocity *= Math.pow(1 - r.O8, t)
  }
  roll(angularVelocity: number) {
    const t = this.cameraModule.getData(!1)
    this.currentOrientation.setFromAxisAngle(Vectors.FORWARD, angularVelocity)
    this.currentOrientation.multiplyQuaternions(t.pose.rotation, this.currentOrientation)
    this.currentOrientation.normalize()
    ;(this.checkBounds && !this.insideBounds(t.pose.position, this.currentOrientation, t.pose.projection)) ||
      t.pose.rotation.equals(this.currentOrientation) ||
      this.cameraModule.updateCameraRotation(this.currentOrientation)
  }
}
